using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTPhieuChuyenKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTPhieuChuyenKho(CTPhieuChuyenKho ctphieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTPhieuChuyenKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTPhieuChuyenKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuchuyenkho.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuChuyenKho", DbType.Int32, ParameterDirection.Input, ctphieuchuyenkho.IdPhieuChuyenKho);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenHang", DbType.String, ParameterDirection.Input, ctphieuchuyenkho.TenHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaHang", DbType.String, ParameterDirection.Input, ctphieuchuyenkho.MaHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctphieuchuyenkho.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctphieuchuyenkho.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctphieuchuyenkho.DonGia);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThanhTien", DbType.Int64, ParameterDirection.Input, ctphieuchuyenkho.ThanhTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctphieuchuyenkho.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTPhieuChuyenKho in DB
		public ResultObj GetAllCTPhieuChuyenKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuChuyenKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuChuyenKho>();
			CTPhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuChuyenKho = Convert.ToInt32(reader["IdPhieuChuyenKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTPhieuChuyenKho in DB with condition
		public ResultObj GetAllCTPhieuChuyenKhoByID(CTPhieuChuyenKho ctphieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuChuyenKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuchuyenkho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuChuyenKho>();
			CTPhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuChuyenKho = Convert.ToInt32(reader["IdPhieuChuyenKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.MaHang = Convert.ToString(reader["MaHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a CTPhieuChuyenKho out of DB with condition
		public ResultObj DeleteCTPhieuChuyenKho(CTPhieuChuyenKho ctphieuchuyenkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTPhieuChuyenKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTPhieuChuyenKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuchuyenkho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhieuChuyenKho from DB with condition
		public ResultObj GetPhieuChuyenKho(int IdPhieuChuyenKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuChuyenKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuChuyenKho", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuChuyenKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuChuyenKho>();
			PhieuChuyenKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuChuyenKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KhoXuat = Convert.ToString(reader["KhoXuat"]);
			item.KhoNhan = Convert.ToString(reader["KhoNhan"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        public ResultObj GetTongHopChuyenKho()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<TongHopChuyenKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetTongHopChuyenKho", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<TongHopChuyenKho>();
                TongHopChuyenKho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new TongHopChuyenKho();
                    item.IdPhieuChuyenKho = Convert.ToInt32(reader["IdPhieuChuyenKho"]);
                    item.TenHang = Convert.ToString(reader["TenHang"]);
                    item.MaHang = Convert.ToString(reader["MaHang"]);
                    item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
                    item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.KhoXuat = Convert.ToString(reader["KhoXuat"]);
                    item.KhoNhan = Convert.ToString(reader["KhoNhan"]);
                    item.LyDo = Convert.ToString(reader["LyDo"]);
                    item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
	}
}
